import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import 'element-ui/lib/theme-chalk/index.css'
import 'highlight.js/styles/atom-one-dark.css'
import '@/styles/common.scss'
import '@/styles/iconfont.css'
import ElementUI from 'element-ui';
import '@/styles/transition.scss'
import NProgress from 'nprogress';
import 'nprogress/nprogress.css'

import Login from '@/service/login'
import JsonExcel from 'vue-json-excel'
Vue.component('downloadExcel', JsonExcel)
import { getToken } from '@/utils/token'
Vue.config.productionTip = false

NProgress.configure({ showSpinner: false });
const whiteList = ['/login']

let loginService = new Login();
router.beforeEach(async (to, from, next) => {
  NProgress.start();
  const token = await loginService.loginCheck();
  if (token.error_code === 0) {
    if (to.path === '/login') {
      next({ path: '/' })
      NProgress.done();
    } else {
      next();
      NProgress.done()
    }
  } else {
    if (whiteList.indexOf(to.path) !== -1) {
      next()
    } else {
      next(`/login`)
      NProgress.done()
    }
  }
})
router.afterEach(() => {
  // finish progress bar
  NProgress.done()
})

Vue.use(ElementUI);

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')
